package com.chuzhiyun.chengguoxy.feature.internship.project;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.GridLayoutManager;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.graphics.Color;
import android.view.View;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.chuzhiyun.chengguoxy.R;
import com.chuzhiyun.chengguoxy.databinding.ActivityProjectListBinding;
import com.chuzhiyun.chengguoxy.feature.base.BaseActivity;
import com.chuzhiyun.chengguoxy.feature.bean.ProjectListBean;
import com.chuzhiyun.chengguoxy.network.NetApi;
import com.chuzhiyun.chengguoxy.network.NetService;
import com.chuzhiyun.chengguoxy.network.StringNetCallback;
import com.chuzhiyun.chengguoxy.utlis.CustomClickListener;
import com.chuzhiyun.chengguoxy.utlis.NewOnItemChildClickListener;
import com.chuzhiyun.chengguoxy.utlis.StringUtils;
import com.chuzhiyun.chengguoxy.utlis.TimeUtils;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadMoreListener;

import org.jetbrains.annotations.NotNull;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@SuppressWarnings("ALL")
public class ProjectListActivity extends BaseActivity<ActivityProjectListBinding> {

    private ProjectListActivity.MyAdapter myAdapter;
    private List<ProjectListBean> listArray = new ArrayList<ProjectListBean>();

    @Override
    public void initView(View view) {
        viewBinding.navView.tvTitle.setText("实习经历");
        viewBinding.navView.ivBack.setOnClickListener(new CustomClickListener() {
            @Override
            protected void onSingleClick() {
                onBackPressed();
            }
        });
        viewBinding.bottomRl.setOnClickListener(new CustomClickListener() {
            @Override
            protected void onSingleClick() {
                Intent intent = new Intent();
                intent.putExtra("typeStr","0");
                intent.setClass(mContext,ProjectInfoActivity.class);
                startActivityForResult(intent, 100);
            }
        });
        viewBinding.refreshLayout.setOnRefreshLoadMoreListener(new OnRefreshLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {

            }

            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                getInternship();
            }
        });
        viewBinding.refreshLayout.setEnableLoadMore(false);
        viewBinding.recyclerView.setLayoutManager(new GridLayoutManager(this, 1));
        myAdapter = new MyAdapter(R.layout.item_work_project, listArray);
        viewBinding.recyclerView.setAdapter(myAdapter);
        myAdapter.addChildClickViewIds(R.id.left_Text);
        myAdapter.addChildClickViewIds(R.id.right_Text);
        myAdapter.setOnItemChildClickListener(new NewOnItemChildClickListener() {
            @Override
            protected void onSingleClick(BaseQuickAdapter adapter, View view, int position) {
                String typeStr = "";
                if (view.getId() == R.id.left_Text){

                }
                if (view.getId() == R.id.right_Text){
                    ProjectListBean bean = listArray.get(position);
                    Intent intent = new Intent();
                    intent.putExtra("typeStr","1");
                    intent.putExtra("id",bean.getId()+"");
                    intent.setClass(mContext,ProjectInfoActivity.class);
                    String statusStr = "";
                    if (StringUtils.isBlank(bean.getEnd())) {
                        boolean isAfter = TimeUtils.TimeCompare(bean.getStart());
                        if (isAfter)
                        {
                            //进行中
                            statusStr = "进行中";
                        }
                        else {
                            //未开始
                            statusStr = "未开始";
                        }
                    }else {
                        //已结束
                        statusStr = "已结束";
                    }
                    intent.putExtra("statusStr",statusStr);
                    startActivity(intent);
                }
            }
        });
    }

    @Override
    public void initData() {

    }

    @Override
    public void setListener() {
        getInternship();
    }

    private void getInternship(){
        String url = NetApi.getProjectList();
        NetService.getInstance().get(url, new StringNetCallback() {
            @Override
            public void onSuccess(String response, int statusCode) throws Exception {
                System.out.println(response);
                viewBinding.refreshLayout.finishRefresh();
                if (statusCode==200){
                    listArray.clear();
                    Gson gson = new Gson();
                    listArray = gson.fromJson(response, new TypeToken<List<ProjectListBean>>() {
                    }.getType());
                    myAdapter.setNewData(listArray);
                    myAdapter.notifyDataSetChanged();
                }
            }

            @Override
            public void onError(String error) {
                System.out.println(error);
                viewBinding.refreshLayout.finishRefresh();
            }
        });
    }

    public static class MyAdapter extends BaseQuickAdapter<ProjectListBean, BaseViewHolder> {

        private MyAdapter(int layoutResId, List<ProjectListBean> data) {
            super(layoutResId, data);
        }

        @Override
        protected void convert(@NotNull BaseViewHolder helper, ProjectListBean item) {
            helper.setText(R.id.projectName_Text,item.getEmployer());
            if (StringUtils.isBlank(item.getEnd())){
                boolean isAfter = TimeUtils.TimeCompare(item.getStart());
                if (isAfter){
                    helper.setText(R.id.statue_Text,"进行中");
                    helper.setTextColor(R.id.statue_Text, Color.parseColor("#14B131"));
                    helper.setText(R.id.time_Text,item.getStart()+" 至 今");
                    helper.setText(R.id.left_Text,"离职");
                    helper.setText(R.id.right_Text,"查看和编辑");
                    helper.setVisible(R.id.left_Text, true);
                    helper.setVisible(R.id.right_Text, true);
                }else {
                    helper.setText(R.id.time_Text,item.getStart());
                    helper.setText(R.id.statue_Text,"待开始");
                    helper.setTextColor(R.id.statue_Text, Color.parseColor("#F0CF0C"));
                    helper.setText(R.id.left_Text,"删除");
                    helper.setText(R.id.right_Text,"查看和编辑");
                    helper.setVisible(R.id.left_Text, true);
                    helper.setVisible(R.id.right_Text, true);
                }
            }
            else {
                //已结束
                helper.setTextColor(R.id.statue_Text, Color.parseColor("#999999"));
                helper.setText(R.id.time_Text,item.getStart()+" 至 "+item.getEnd());
                helper.setText(R.id.statue_Text,"已结束");
                helper.setText(R.id.right_Text,"查看");
                helper.setGone(R.id.left_Text,true);
                helper.setVisible(R.id.right_Text, true);
            }
        }
    }

    @Override
    protected ActivityProjectListBinding getViewBinding() {
        return ActivityProjectListBinding.inflate(getLayoutInflater(), baseBinding.getRoot(), true);
    }

}